class Context {
  public service:any;
  public controller:any;
  public routers:Map;
  public urlPatterns:Map;
  constructor() {
    this.service = {};
    this.controller = {};
    this.routers = new Map();
    this.urlPatterns = new Map();
  }
  addService(name, service) {
    this.service[`${name}`] = service;
  }
  addController(name, controller) {
    this.controller[`${name}`] = controller;
  }
  addRouter(router) {
    for (const routerKey in router) {
      this.routers.set(routerKey,router[routerKey]);
    }
  }
  matchUrl(url) {
    for (let [key, value] of this.urlPatterns) {
      if (value.test(url)) {
        let match = value.exec(url);
        return { router: this.routers.get(key), groups: match.pathname.groups };
      }
    }
    return null;
  }
  buildUrlPatterns() {
    for (let [key, value] of this.routers) {
      this.urlPatterns.set(key, new URLPattern({ pathname: key }));
    }
  }
}
export default Context;